		linklist
		symbols
		debug	asm
;6502 simulator example program
;
eos:		equ	0		;end of string marker
cr:		equ	0dh		;carriage return
in_port:	equ	3eh		;input port
out_port:	equ	3fh		;output port
;
		PAGE0
		org	40h
msg:		db	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
cmd_str:	db	"Hello World!   ",eos	;command string
inp_buffer:	ds	16		;input buffer
have_line:	db	0		;have a line flag
buf_index:	db	0		;input buffer index

		DATA
		org	100h
sys_stk:	ds	ffh		;system stack area
sys_stk_end:	equ	$

		CODE
		org	200h
;
start:		ldx	#ffh		;setup system stack pointer
		txs
		lda	#0
		sta	have_line	;clear have_line flag
		sta	buf_index	;clear buffer index
		cli			;enable irq
wait:		lda	have_line	;check for input line
		beq	wait		;not done yet
		sei			;disable irq
		lda	#0		;zero a reg for sim display
got_line:	jsr	compare		;compare input line to cmd_str
done:		nop
		jmp	start		;repeat
;
;routine to compare two strings
;returns 1 if the strings are equal else returns 0
;the return value is in the A register
;
compare:	ldx	#0
		ldy	#0
cmp_loop:	lda	inp_buffer,x	;get character from string 1
		cmp	cmd_str,y	;compare to string 2 character
		beq	cmp_eos		;if equal check for end of string
		lda	#0		;string 1 does not equal string 2
		beq	prnt_msg	;all done
cmp_eos:	inx			;increment buffer index first
		iny	
		cmp	#eos		;end of string ?
		bne	cmp_loop	;check next character
		lda	#1		;set return value
prnt_msg:	ldx 	#0
		ldy	#0
		cmp	#1
		bne	fail_msg
pass_loop:	lda	pass,x
		sta	msg,x
		inx
		cpx	#6
		bne	pass_loop
		jmp	cmp_done
fail_msg:	lda	fail,x  
		sta	msg,x
		inx
		cpx	#6
		bne	fail_msg
cmp_done:	rts
;
;irq interrupt service routine
;returns no value
;
irq_int:	pha			;save accumulator
		txa			
		pha			;save X
		tya			
		pha			;save Y
		ldx	buf_index	;get buffer index
		lda	in_port		;get input byte
		sta	out_port	;echo it
		cmp	#cr		;input byte = carriage return ?
		bne	more_inp
		inc	have_line	;mark as having a line
		lda	#eos		;convert carriage return to eos
more_inp:	sta	inp_buffer,x	;store byte in buffer
		inx			;increment buffer index
		stx	buf_index	;save new buffer index
		pla			;restore registers
		tay
		pla	
		tax
		pla
		rti
;
pass:		db	"Passed"
fail:		db	"Failed"

;
		org	fffeh
;
		dw	irq_int		;irq interrupt vector
;
		org	fffch
;
		dw	start		;reset vector
;
		end	start

